/**
 * Copyright (C) 2011 Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using Plurk;

void main(string[] args)
{
    string error_text;
    Client plurk;

    if (args.length == 3) {
        plurk = new Client(args[1], args[2]);
        string token = plurk.request_token(out error_text);

        if (token == null) {
            stdout.printf("error_text: %s\n", error_text);
            return;
        }

        stdout.printf("Resource Owner Authorization: %s\n", token);
        stdout.printf("Please enter the verification code: ");

        string code = stdin.read_line();

        if (plurk.access_token(code, out error_text) == true) {
            stdout.printf("The verification code is OK.\n");
        } else {
            stdout.printf("error_text: %s\n", error_text);
            return;
        }
    } else {
        plurk = new Client(args[1], args[2], args[3], args[4]);
    }

    if (args.length == 6) {
        var data = plurk.add(args[5], "says", null, null, "tr_ch", out error_text);
        if (data != null) {
            string qualifier = data.lookup("qualifier_translated") ?? data.lookup("qualifier");
            string content = data.lookup("content_raw");
            string posted = data.lookup("posted");
            stdout.printf("%s %s at %s\n", qualifier, content, posted);
        } else {
            stdout.printf("error_text: %s\n", error_text);
        }
        return;
    }

    HashTable<string,int> unread;

    if (plurk.get_unread_count(out unread))
        stdout.printf("all unread: %d\n", unread.lookup("all"));

    User profile = plurk.get_own_profile();

    if (profile != null) {
        stdout.printf("id:           %s\n", profile.lookup("id"));
        stdout.printf("karma:        %s\n", profile.lookup("karma"));
        stdout.printf("nick_name:    %s\n", profile.lookup("nick_name"));
        stdout.printf("display_name: %s\n", profile.lookup("display_name"));

        List<unowned Data> plurks = profile.get_plurks();

        foreach (unowned Data data in plurks) {
            int i = 0;
            User user = profile.get_user(data.lookup("owner_id"));
            string name = user.lookup("display_name") ?? user.lookup("nick_name");
            string qualifier = data.lookup("qualifier_translated") ?? data.lookup("qualifier");
            string content = data.lookup("content_raw");
            stdout.printf("%s[%s] %s %s\n", name, user.lookup("nick_name"), qualifier, content);
        }
    }

    //GLib.mem_profile();
}
/* -*- coding: utf-8; indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- */
/* vim:set fileencodings=utf-8 tabstop=4 expandtab shiftwidth=4 softtabstop=4: */
